package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.BannerClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Mr.Alan
 * @since 2020-08-16
 */
@RestController
@RequestMapping
@CrossOrigin
public class IndexController {

    @Resource
    private EduCourseService eduCourseService;
    @Resource
    private EduTeacherService eduTeacherService;
    @Resource
    private BannerClient bannerClient;

    @ApiOperation("获取首页的教师和课程推荐")
    @PostMapping("/eduService/index/data")
    public R getTeacherAndCourse() {
        List<EduCourse> courses =
                eduCourseService.lambdaQuery().eq(EduCourse::getStatus, "Normal").select(EduCourse::getId,
                        EduCourse::getTitle, EduCourse::getCover, EduCourse::getPrice,
                        EduCourse::getViewCount, EduCourse::getBuyCount).orderByAsc(EduCourse::getViewCount).last("limit 8").list();
        List<EduTeacher> teachers = eduTeacherService.lambdaQuery().orderByAsc(EduTeacher::getId).last("limit 4").list();
        return R.ok().data("courses", courses).data("teachers", teachers);
    }

    @PostMapping("/cmsService/user/banner/list")
    @ApiOperation("首页轮播图")
    public R listBanner() {
        return bannerClient.listBanner();
    }

}
